package com.example.shop_2207.fragment

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.shop_2207.entity.CategoryEntity
import com.example.shop_2207.entity.CategoryEntityResult
import com.example.shop_2207.entity.GoodsEntityResult
import com.example.shop_2207.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class CateViewModel:BaseViewModel() {
    //请求一级二级分类数据----分类接口
    val cateSuccess = MutableLiveData<CategoryEntityResult>()
    val cateFail = MutableLiveData<String>()
    fun cate(){
        viewModelScope.launch {
            try {
                val result = apiServer.cate()
                cateSuccess.value = result
            }catch (e:Exception){
                cateFail.value = e.message
            }
        }
    }
    //请求三级列表的数据----商品接口
    val goodsSuccess = MutableLiveData<GoodsEntityResult>()
    val goodsFail = MutableLiveData<String>()
    fun goods(category_id :Int,currentPage:Int,pageSize:Int){
        viewModelScope.launch {
            try {
                val result = apiServer.goods(category_id, currentPage, pageSize)
                goodsSuccess.value = result
            }catch (e:Exception){
                goodsFail.value = e.message
            }
        }
    }
}